package com.serkol.ams.controller.api;


import cn.binarywang.wx.miniapp.api.WxMaService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.UsersAddress;
import com.serkol.ams.entity.UsersAuthentication;
import com.serkol.ams.service.UsersAddressService;
import com.serkol.ams.service.UsersAuthenticationService;
import com.serkol.ams.service.UsersService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultCode;
import com.serkol.constant.ResultConstant;
import com.serkol.exception.AuthException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/users/address")
public class UsersAddressApiController {


    @Autowired
    private UsersService userService;


    @Autowired
    private UsersAddressService addressService;
    @Autowired
    private UsersAuthenticationService authenticationService;
    @Autowired
    private WxMaService wxMaService;
    @IsLogin
    @GetMapping("/findByPageList")
    public Result findByPageList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current
            , @RequestParam(name = "userId", defaultValue = "0") Long userId) {
        Result result = new Result();
        MyPageOld list = addressService.findByList(pageSize,current, userId);
        if (list.getSize() > 0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }
    @IsLogin
    @GetMapping("/info")
    @ResponseBody
    public Result info() throws AuthException {
        UsersAuthentication usersAuthentication = authenticationService.getInfo();
        if (usersAuthentication != null) {
            return Result.ok(usersAuthentication);
        } else {
            throw new AuthException(ResultCode.OK_CONTENT_NO);
        }
    }
    @IsLogin
    @GetMapping("/findById")
    @ResponseBody
    public Result findById(@RequestParam(value = "id", defaultValue = "0") long id) throws AuthException {

        if (id > 0) {
            UsersAddress byId = addressService.getById(id);
            return Result.ok(byId);
        } else {
            throw new AuthException(ResultCode.OK_CONTENT_NO);
        }
    }

    @IsLogin
    @GetMapping("/findByList")
    @ResponseBody
    public Result findByList() {
        LoginLog loginLog = AuthConstant.getLoginLog();
        QueryWrapper<UsersAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UsersAddress::getUserId, loginLog.getUserId()).orderByDesc(UsersAddress::isMoren, UsersAddress::getDate);
        List<UsersAddress> list = addressService.list(queryWrapper);
        return Result.ok(list);
    }
    @IsLogin
    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody UsersAddress usersAddress) throws AuthException {
        //   System.out.println(usersAuthentication);
        LoginLog loginLog = AuthConstant.getLoginLog();
        //  usersAuthentication.setUserId(loginLog.getUserId());
        System.out.println(usersAddress);
        usersAddress.setUserId(loginLog.getUserId());
        boolean b = false;
        if (usersAddress.getId() != null && usersAddress.getId() > 0) {
            usersAddress.setId(usersAddress.getId());
            b = addressService.updateById(usersAddress);
        } else {
            b = addressService.save(usersAddress);
        }
        LambdaUpdateWrapper<UsersAddress> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.set(UsersAddress::isMoren, false);
        queryWrapper.eq(UsersAddress::getUserId, loginLog.getUserId()).notIn(UsersAddress::getId, usersAddress.getId());
        addressService.update(queryWrapper);
        if (b) {
            return Result.ok();
        } else {
            throw new AuthException(ResultCode.REQUEST_ERROR_PRECONDITION_FAILED);
        }
    }
    @IsLogin
    @PostMapping("/byDelete")
    @ResponseBody
    public Result byDelete(@RequestParam(value = "id", defaultValue = "0") long id) throws AuthException {
        if(id>0) {
            boolean b = addressService.byDelete(id);
            if (b) {
                return Result.ok();
            } else {
                throw new AuthException(ResultCode.REQUEST_ERROR_NOT_ACCEPTABLE);
            }
        }else{
            throw new AuthException(ResultCode.REQUEST_ERROR_NOT_FOUND);
        }
    }

}
